//
//  HelpCenterViewController.m
//  Telemedicine
//
//  Created by Trong Ton on 5/16/14.
//  Copyright (c) 2014 Trong Ton. All rights reserved.
//

#import "HelpCenterViewController.h"
#import "JMImageCache.h"

@interface HelpCenterViewController ()

@end

@implementation HelpCenterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bottom.jpg"]];
    
    NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    int startYView = ([[vComp objectAtIndex:0] intValue] >= 7)?20:0;
    
    UIScrollView *contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, startYView, 320, self.view.bounds.size.height - 49 -startYView)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    [contentView release];
    
    UIImageView *bigImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, contentView.bounds.size.width, 218)];
    NSString *linkImage = [[NSUserDefaults standardUserDefaults] objectForKey:@"BackgroundImageLink"];
    if (linkImage && linkImage.length > 0) {
        [bigImage setImageWithURL:[NSURL URLWithString:linkImage] placeholder:nil];
    }
    else {
        [bigImage setImage:[UIImage imageNamed:@"background_call_default.jpg"]];
    }
    [contentView addSubview:bigImage];
    [bigImage release];
    
    int startY = 218;
    UIImageView *introImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, startY, contentView.bounds.size.width, 48)];
    introImage.image = [UIImage imageNamed:@"supporting_requestcode.png"];
    [contentView addSubview:introImage];
    [introImage release];
    
    startY += 48;
    UIButton *sendEmailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendEmailButton.frame = CGRectMake(0, startY, contentView.bounds.size.width, 48);
    [sendEmailButton setBackgroundImage:[UIImage imageNamed:@"supporting_email_normal.png"] forState:UIControlStateNormal];
    [sendEmailButton addTarget:self action:@selector(sendEmailPress:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:sendEmailButton];
    
    startY += 48 + 10;
    UIButton *callSupportButton = [UIButton buttonWithType:UIButtonTypeCustom];
    callSupportButton.frame = CGRectMake(0, startY, contentView.bounds.size.width, 48);
    [callSupportButton setBackgroundImage:[UIImage imageNamed:@"supporting_phone_normal.png"] forState:UIControlStateNormal];
    [callSupportButton addTarget:self action:@selector(callSupportPress:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:callSupportButton];
    
    startY += 48;
    if(startY < contentView.bounds.size.height - 23) {
        startY = contentView.bounds.size.height - 23;
    }
    UIImageView *phoneInfo = [[UIImageView alloc] initWithFrame:CGRectMake(0, startY, contentView.bounds.size.width, 23)];
    [phoneInfo setImage:[UIImage imageNamed:@"home_bottomwebsite_url.jpg"]];
    [contentView addSubview:phoneInfo];
    [phoneInfo release];
    
//    [contentView setContentSize:CGSizeMake(contentView.bounds.size.width, (startY + 25))];//startY+bottomView.bounds.size.height)];
//    contentView.scrollEnabled = YES;
//    UIImageView *advertiseBanner1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, startY, contentView.bounds.size.width, 48)];
//    [advertiseBanner1 setImage:[UIImage imageNamed:@"home_minduration_banner.jpg"]];
//    [contentView addSubview:advertiseBanner1];
//    [advertiseBanner1 release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendEmailPress:(id)sender {
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    if ([MFMailComposeViewController canSendMail]) {
        NSArray *usersTo = [NSArray arrayWithObject: @"media@md24housecall.com"];
        [mailViewController setToRecipients:usersTo];
        [mailViewController setSubject:@"MD24 Registration Email"];
        [mailViewController setMessageBody:@"First name:\nLast name:\nCell phone:\nHome phone:\nFull address:\nLeave your comment here:" isHTML:NO];
        [[AppDelegate sharedDelegate].window.rootViewController presentModalViewController:mailViewController animated:YES];
    }
    [mailViewController release];
}

- (void)callSupportPress:(id)sender {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:8886324758"]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:8886324758"]];
    }
    else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert !" message:@"Don't make a call with this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }
}

#pragma mark - Sent mail

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error  {
    NSString *message = @"";
	// Notifies users about errors associated with the interface
	switch (result)
	{
		case MFMailComposeResultCancelled:
			//message = NSLocalizedString(@"alert.message.warning.mail.photo.cancelled", @"");
            break;
		case MFMailComposeResultSaved:
			//message = NSLocalizedString(@"alert.message.success.mail.photo.saved", @"");
			break;
		case MFMailComposeResultSent:
        {
			message = @"Mail was sent";
			break;
        }
		case MFMailComposeResultFailed:
			message = @"An unknow error has occurred, there might be problems with the data format from our server. Please try again in few seconds.";
			break;
		default:
			break;
	}
    if (message.length > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        
        
    }
	[[AppDelegate sharedDelegate].window.rootViewController dismissModalViewControllerAnimated:YES];
}
@end
